473,417 Members | 1,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,417 software developers and data experts.

Opening a file in visual c# not a valid filename

I am using an openfiledialoge to open a dbf file in a windows app. the error I get is c:\myfile.dbf not an valid filename. here is my code
Expand|Select|Wrap|Line Numbers
  1. private void button1_Click ( object sender , EventArgs e )
  2.             {
  3.             System.Windows.Forms.DataGrid ZipCode=null; 
  4.             System . IO . Stream myStream = null;
  5.             OpenFileDialog openFileDialog1 = new OpenFileDialog ( );
  6.             openFileDialog1 . InitialDirectory = "c:\\";
  7.             openFileDialog1 . Filter = "dbf files (*.dbf)|*.dbf";
  8.             openFileDialog1 . FilterIndex = 0;
  9.             openFileDialog1 . RestoreDirectory = true;
  10.             openFileDialog1 . CheckFileExists = true;
  11.             openFileDialog1 . CheckPathExists = true;
  12.             if ( openFileDialog1 . ShowDialog ( ) == DialogResult . OK )
  13.                 {
  14.                 try
  15.                     {
  16.                     if ( ( myStream = openFileDialog1 . OpenFile ( ) ) != null )
  17.                         {//if
  18.                         using ( myStream )
  19.                             {//using
  20.                         string fullPathname = openFileDialog1 . FileName;
  21.                         string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\;Extended Properties=dBASE IV;User ID=Admin;Password=";
  22.                         string commandString = "Select * from '" + fullPathname + "'";
  23.                         OleDbDataAdapter DataAdapter = new OleDbDataAdapter (commandString,connectionString );
  24.                         DataSet DataSet = new DataSet ( );
  25.                         DataAdapter . Fill ( DataSet , fullPathname );
  26.               ZipCode.DataSource = DataSet . Tables [ fullPathname ] . DefaultView;
  27.                             } //end using
  28.                         } //end if                    
  29.                  }catch( Exception ex )
  30.                     {
  31.                     MessageBox . Show ( "  error: " + ex . Message );
  32.                     }
  33.                 }
  34.             }
  35.  
Nov 3 '07 #1
6 5733
balabaster
797 Expert 512MB
I am using an openfiledialoge to open a dbf file in a windows app. the error I get is c:\myfile.dbf not an valid filename. here is my code
<code>
private void button1_Click ( object sender , EventArgs e )
{
System.Windows.Forms.DataGrid ZipCode=null;
System . IO . Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog ( );
openFileDialog1 . InitialDirectory = "c:\\";
openFileDialog1 . Filter = "dbf files (*.dbf)|*.dbf";
openFileDialog1 . FilterIndex = 0;
openFileDialog1 . RestoreDirectory = true;
openFileDialog1 . CheckFileExists = true;
openFileDialog1 . CheckPathExists = true;
if ( openFileDialog1 . ShowDialog ( ) == DialogResult . OK )
{
try
{
if ( ( myStream = openFileDialog1 . OpenFile ( ) ) != null )
{//if
using ( myStream )
{//using
string fullPathname = openFileDialog1 . FileName;
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\;Extended Properties=dBASE IV;User ID=Admin;Password=";
string commandString = "Select * from '" + fullPathname + "'";
OleDbDataAdapter DataAdapter = new OleDbDataAdapter (commandString,connectionString );
DataSet DataSet = new DataSet ( );
DataAdapter . Fill ( DataSet , fullPathname );
ZipCode.DataSource = DataSet . Tables [ fullPathname ] . DefaultView;
} //end using
} //end if
}catch( Exception ex )
{
MessageBox . Show ( " error: " + ex . Message );
}
}
}
</code>
Well my first thought is that the file doesn't exist...
Nov 3 '07 #2
Well my first thought is that the file doesn't exist...
the fille exists it is a .dbf file from the usps. I have been debugging and the line that triggers the exception is
Expand|Select|Wrap|Line Numbers
  1. ZipCode.DataSource = DataSet . Tables [ fullPathname ] . DefaultView;
when I put a watch on the line it says the DataSet.Tables[fullPathname] is null
thanks for your time
John Larson
Nov 4 '07 #3
balabaster
797 Expert 512MB
the fille exists it is a .dbf file from the usps. I have been debugging and the line that triggers the exception is
Expand|Select|Wrap|Line Numbers
  1. ZipCode.DataSource = DataSet . Tables [ fullPathname ] . DefaultView;
when I put a watch on the line it says the DataSet.Tables[fullPathname] is null
thanks for your time
John Larson
Does your database have a table in it with the same name as your DBF? I've gotta say I'm clueless on DBase so I'm running blind - I only have MS Access and SQL Server and my example works...
VB
Expand|Select|Wrap|Line Numbers
  1. Dim fileName As String = "C:\MyDB.accdb" 
  2. Dim tableName As String = "MyTable"
  3. Dim sConStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & fileName & ";Persist Security Info=False;"
  4. Dim oDA As New OleDbDataAdapter("Select * From " & tableName, sConStr)
  5. Dim oDS As New DataSet("MyDataSet")
  6. oDA.Fill(oDS, "MyTable")
  7. ListBox1.DataSource = oDS.Tables("MyTable")
  8.  
C#
Expand|Select|Wrap|Line Numbers
  1. string fileName = "C:\\MyDB.accdb"; 
  2. string tableName = "MyTable";
  3. string sConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Persist Security Info=False;
  4. OleDbDataAdapter oDA = New OleDbDataAdapter("Select * From " + tableName, sConStr);
  5. DataSet oDS = New DataSet("MyDataSet");
  6. oDA.Fill(oDS, "MyTable");
  7. ListBox1.DataSource = oDS.Tables["MyTable"];
  8.  
So given what you've said, I'm curious if the structure of your database is as you expect...or that you've overlooked something in that area.
Nov 4 '07 #4
I am using the same file path as you are and my application cannot find the file. I have changed a lot of code this is what I am trying to make work
Expand|Select|Wrap|Line Numbers
  1. try
  2.          {
  3.          string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\ZIP.DBF;Extended Properties=dBase IV";
  4.          OleDbConnection conn = new OleDbConnection ( conString );
  5.          command = conn . CreateCommand ( );
  6.  
  7.          // create the DataSet
  8.          DataSet ds = new DataSet ( );
  9.          dataGridView1 . DataSource = null;
  10.  
  11.          // open the connection
  12.          conn . Open ( );
  13.          string commandString = "Select * from  ZIP.DBF";
  14.          // run the query
  15.          command . CommandText = commandString;
  16.          OleDbDataAdapter adapter = new OleDbDataAdapter ( command );
  17.          adapter . Fill ( ds );
  18.  
  19.          // close the connection
  20.          conn . Close ( );
  21.  
  22.          // set the grid's data source
  23.          dataGridView1 . DataSource = ds . Tables [ 0 ];
  24.          }
  25.      catch ( Exception ex)
  26.          {
  27.          MessageBox . Show (  ex . Message );
  28.  
  29.          }             
  30.  
I have also downloaded a shareware dbf viewer and it opens the file and correctly displays the file columns and rows
john larson
thanks for the time
Nov 5 '07 #5
I have cured this problem with this line
Expand|Select|Wrap|Line Numbers
  1. fullPath2 = Path . GetDirectoryName ( openFileDialog1 . FileName );
then I feed fullpath2 into my connection string and It worked. thanks for all of your help
John Larson
Nov 7 '07 #6
Plater
7,872 Expert 4TB
I have cured this problem with this line
Expand|Select|Wrap|Line Numbers
  1. fullPath2 = Path . GetDirectoryName ( openFileDialog1 . FileName );
then I feed fullpath2 into my connection string and It worked. thanks for all of your help
John Larson
I was going to say your connection string does not point to a database, just a path.
Nov 7 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Vinay | last post by:
Hi I have a corrupt word file. I am able to open it with the code given below tr Dim pInfo As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo( pInfo.UseShellExecute...
14
by: D. Alvarado | last post by:
Hello, I am trying to open a window containing an image and I would like the image to be flush against the window -- i.e. have no padding or border. Can I make this happen with a single call to a...
0
by: Sarah | last post by:
Hi, I'm opening a MS Word file through c# // Open the specified document DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory); FileInfo fi = di.GetFiles("myTest.doc"); if...
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
9
by: Rocky | last post by:
I have 2 textboxes. When I click submit, i want to add whatevers in the text box1 as username and whatevers in textbox2 as userid into an xml file. How do I do that in ASP.NET using vb.net? My...
11
by: aldrin | last post by:
I'm trying to run this code under windows xp sp2 using codeblocks v1.0 compiler with great difficulty.There is no problem with running this under KDevelop in linux. Any help would be greatly...
4
by: Charlie Brookhart | last post by:
I am trying to write a code for a button click event. When the button is clicked, it is supposed to bring up an open file dialog box to allow the user to select the document they which to open....
2
by: Craig | last post by:
Hi there, I'm trying to open colour BMPs using PIL and I'm getting the following errors. Opening a 16 colour BMP I get: Traceback (most recent call last): File "<pyshell#3>", line 1, in...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.